package controller_admin

import (
	"github.com/doujunyu/gogo/job"
	"go_self/server"
	"strconv"
	"time"
)

// CategoryByList 种类列表
func CategoryByList(j *job.Job) {
	data := server.CategoryByList(j.Input)
	if len(data) == 0 {
		j.JsonSuccess()
		return
	}
	j.JsonSuccess(data)
	return
}

// CategoryByAdd 种类添加
func CategoryByAdd(j *job.Job) {
	timestamp := time.Now().Format(time.RFC3339)
	insertData := make(map[string]interface{})
	insertData["title"] = j.Input["title"]
	insertData["ctime"] = timestamp
	insertData["utime"] = timestamp
	err := server.CategoryByAdd(insertData)
	if err != nil {
		j.JsonError(nil, err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// CategoryByEdit 种类编辑
func CategoryByEdit(j *job.Job) {
	id := j.Input["id"]
	title := j.Input["title"]
	if id == "" || title == "" {
		j.JsonError(nil, "缺少参数")
	}
	err := server.CategoryByEdit(id, title)
	if err != nil {
		j.JsonError(nil, err.Error())
		return
	}
	j.JsonSuccess()
	return
}

// CategoryByDel 种类删除
func CategoryByDel(j *job.Job) {
	if j.Input["id"] == "" {
		j.JsonError(nil, "缺少参数")
	}
	id, _ := strconv.Atoi(j.Input["id"])
	err := server.CategoryByDel(id)
	if err != nil {
		j.JsonError(nil, err.Error())
		return
	}
	j.JsonSuccess()
	return
}
